package com.woniuxy.shop.dao;

import java.util.ArrayList;
import java.util.List;

import com.woniuxy.shop.entity.Order;

/**
*@author 作者：
*version 创建时间：
*类说明：
*/
public class OrderDao {
	
	/**
	 * 增加订单信息
	 * @param o
	 */
	public void add(Order o) {
		String sql = "insert into shop_order(orderno,userid,ordertime,receiveinfo,money,paytype,paytime,status) values(?,?,?,?,?,?,?,?)";
		Object[] obs = {o.getOrderno(),o.getUserid(),o.getOrdertime(),o.getReceiveinfo(),o.getMoney(),o.getPaytype(),o.getPaytime(),o.getStatus()};
		DbHelper.excuteSQL(sql, obs);
	}
	
	/**
	 * 修改指定订单编号的收货信息
	 * @param userid
	 * @param receiveinfo
	 */
	public void updateReceiveinfo(int orderno,String receiveinfo) {
		String sql = "update shop_order set receiveinfo=? where orderno=?";
		DbHelper.excuteSQL(sql, receiveinfo,orderno);
	} 
	
	/**
	 * 修改指定订单编号的状态
	 * @param id
	 * @param status
	 * @param paytime 
	 */
	public void updateStatus(int id,String status, String paytime) {
		String sql = "update shop_order set paytime=?,status=? where id=?";
		DbHelper.excuteSQL(sql, paytime,status,id);
	}
	
	/**
	 * 修改指定订单编号的信息
	 * @param orderno
	 */
	public void delete(int orderno) {
		String sql = "delete from shop_order where orderno=?";
		DbHelper.excuteSQL(sql, orderno);
	}
	
	/**
	 * 查询所有订单信息
	 * @return
	 */
	public List<Order> getOrders(){
		List<Order> orders = new ArrayList<>();
		String sql = "select * from shop_order";
		orders = DbHelper.executeQuery(sql, Order.class);
		return orders;
	}
	
	/**
	 * 查询指定id的信息
	 * @param orderno
	 * @return
	 */
	public Order getOrder(int id) {
		String sql = "select o.*,u.account uname from shop_order o ,user_info u where o.id=? and u.id=o.userid";
		return DbHelper.executeQueryForSingleRow(sql, Order.class, id);
	}
	
	/**
	 * 获得最新订单ID
	 * @return
	 */
	public int getMaxID() {
		String sql = "select max(id)from shop_order";
		return DbHelper.executeQueryForScalar(sql);
	}
	
	/**
	 * 多条件分页查询data
	 * @param pagesize
	 * @param page
	 * @param o
	 * @return
	 */
	public List<Order> getOrdersByPage(int pagesize,int page,Order o) {
		String sql = "select o.*,u.account uname from shop_order o,user_info u where o.userid=u.id";
		if(o.getOrderno()!=null&&!o.getOrderno().trim().equals("")) {
			sql += " and orderno like'%"+o.getOrderno()+"%'";
		}
		if(o.getPaytype()!=null&&!o.getPaytype().trim().equals("")) {
			sql += " and paytype='"+o.getPaytype()+"'";
		}
		if(o.getStarttime()!=null&&!o.getStarttime().trim().equals("")) {
			sql +=" and ordertime>='"+o.getStarttime()+"'";
		}
		if(o.getEndtime()!=null&&!o.getEndtime().trim().equals("")) {
			sql +=" and ordertime<='"+o.getEndtime()+"'";
		}
		if(o.getStatus()!=null&&!o.getStatus().equals("")) {
			sql += " and o.status='"+o.getStatus()+"'";
		}
		
		sql += " limit ?,?";
		return DbHelper.executeQuery(sql, Order.class, (page-1)*pagesize,pagesize);
	}
	
	/**
	 * 多条件查询data总数
	 * @param pagesize
	 * @param page
	 * @param o
	 * @return
	 */
	public int getOrdersAsLimit(Order o) {
		String sql = "select count(*) from shop_order where 1 and 1";;
		if(o.getOrderno()!=null&&!o.getOrderno().trim().equals("")) {
			sql += " and orderno like'%"+o.getOrderno()+"%'";
		}
		if(o.getPaytype()!=null&&!o.getPaytype().trim().equals("")) {
			sql += " and paytype='"+o.getPaytype()+"'";
		}
		if(o.getStarttime()!=null&&!o.getStarttime().trim().equals("")) {
			sql +=" and ordertime>='"+o.getStarttime()+"'";
		}
		if(o.getEndtime()!=null&&!o.getEndtime().trim().equals("")) {
			sql +=" and ordertime<='"+o.getEndtime()+"'";
		}
		if(o.getStatus()!=null&&!o.getStatus().equals("")) {
			sql += " and status='"+o.getStatus()+"'";
		}
		return ((Long)DbHelper.executeQueryForScalar(sql)).intValue();
	}
	
	/**
	 * 查询最近5条订单待付款
	 * @param userid
	 * @return
	 */
	public List<Order> getRecentOrders(int userid) {
		String sql  = "select * from shop_order where userid = ? and status='待付款'  order by ordertime desc limit 0,5";
		return DbHelper.executeQuery(sql, Order.class, userid);
	}

	
	/**
	 * 更改订单状态
	 * @param status
	 * @param orderid
	 */
	public void send(String status,int orderid) {
		String sql = "update shop_order set status=? where id=?";
		DbHelper.excuteSQL(sql,status,orderid);
		
	}
}
